import { defineStore } from 'pinia';
// 定义State
interface State {
    count: number;
    userInfo: Object
}

export const useCounterStore = defineStore('counter', {
    // 返回State类型的返回值
    state: (): State => ({
        count: 0,
        userInfo: {
            state: false
        }
    }),
    getters: {
        doubleCount: (state) => state.count * 2,
    },
    actions: {
        increment() {
            this.count++;
        },
        decrement() {
            this.count--;
        },
        /**
         * counterStore.updateParam([{'count': 6}]) 直接赋值给属性
         * counterStore.updateParam(['userInfo', {'state': true}]) // 赋值对象类型
         */
        updateParam(param: any) {
            let attr, data;
            if (param.length > 1) {
                ;
                [attr, data] = param;
            } else {
                ;
                [data] = param;
            }
            const keys = Object.keys(data);
            for (const key of keys) {
                console.log('this', this, key, attr);
                console.log('this666', this.count);
                if (attr) {
                    (this as any)[attr][key] = data[key];
                } else {
                    (this as any)[key] = data[key];
                }
            }
        }
    },
});

export {};